part of _;

extension on UserInfoPage {
  Widget view_avatar() {
    return YButton(
      borderRadius: BorderRadius.circular(50),
      child: YImageCircle(100, 100, globalUserModel!.avatar),
      onTap: () async {
        final result_select = (await AssetUtils.selectAssets(requestType: RequestType.image))?.firstOrNull;
        if (result_select == null) return;
        final result_croppy = await AssetUtils.cropImage(context: context, path: (await result_select.file)!.path);
        if (result_croppy == null) return;
        yShowProgress();
        YOSS.upload(
          filepaths: [result_croppy.path],
          dir: "user_avatar",
          onFinish: (isSuccess, urls, error) {
            yDismissProgress();
            if (!isSuccess) {
              yToast(error!);
              return;
            }
            yPost(IFinal.Jkids.updateAvatar, {
              "avatar": urls!.first,
            }, (isSuccess, results, code, message) {
              if (!isSuccess) return;
              globalUserModel!.avatar = urls.first;
              AppUtils.saveUserModel(globalUserModel!);
              yToast("头像修改成功");
            });
          },
        );
      },
    );
  }

  Widget view_name() {
    return YButton(
      isInk: true,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(globalUserModel!.name),
          Icon(Icons.chevron_right, color: Colors.grey),
        ],
      ),
      onTap: () {
        final textController = TextEditingController(text: globalUserModel!.name);
        yShowDialog(
          alertBuilder: (pageContext, dialogContext) {
            return AlertDialog(
              title: Text("编辑昵称"),
              content: TextFormField(
                controller: textController,
                decoration: InputDecoration(hintText: "请输入新昵称"),
                maxLength: 10,
              ),
              actions: [
                YButton(child: Text("取消"), onTap: () => yDismissDialog(dialogContext)),
                YButton(
                  child: Text("确定"),
                  onTap: () {
                    yCloseInputMethod();
                    final name = textController.text;
                    if (name.isEmpty) {
                      yToast("昵称不能为空");
                      return;
                    }
                    if (name == globalUserModel!.name) {
                      yToast("新昵称不能一样");
                      return;
                    }
                    yPost(IFinal.Jkids.updateName, {
                      "name": name,
                    }, (isSuccess, results, code, message) {
                      if (!isSuccess) return;
                      yDismissDialog(dialogContext);
                      globalUserModel!.name = name;
                      AppUtils.saveUserModel(globalUserModel!);
                      yToast("昵称修改成功");
                    });
                  },
                ),
              ],
            );
          },
        );
      },
    );
  }
}
